package exercicios.ex08.view;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import exercicios.ex08.model.Comentario;
import exercicios.ex08.model.FaceNoteFachada;
import exercicios.ex08.model.Post;

/**
 * Servlet implementation class MainPage
 */
@WebServlet("/mainPage")
public class MainPage extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public MainPage() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		response.setContentType("text/html;charset=utf-8");
		out.println("<html>\n"+
					"<head>\n"+
					"<meta charset=\"utf-8\">\n"+
					"<title>FaceNote Main Page</title>\n"+
					"</head>\n"+
					"<body>\n");
		exibeFormStatus(out);
		exibePosts(out);
		out.println("</body>\n"+
					"</html>");
		out.close();
	}

	private void exibeFormStatus(PrintWriter out) {
		out.println("<form action=\"/exemploServlet/addPost\" method=\"post\">\n"+
					"<b>Autor:</b> <input type=\"text\" name=\"autor\"><br>\n"+
					"<b>Status:</b><br/>\n"+
					"<input type=\"text\" size=\"60\" name=\"status\" value=\"No que você está pensando?\"><br>\n"+
					"<input type=\"submit\" value=\"Publicar\">\n"+
					"</form>\n");
		
	}
	
	private void exibePosts(PrintWriter out) {
		FaceNoteFachada face = FaceNoteFachada.getInstance();
		
		out.println("<table width=\"100%\">\n"+
					"<tr><td width=\"30%\">&nbsp;</td>\n"+
					"<td width=\"40%\">&nbsp;</td>\n"+
					"<td width=\"30%\">&nbsp;</td></tr>");
		List<Post> posts = face.getPosts();
		for (Post post : posts) {
			out.println("<tr><td>&nbsp;</td>\n"+
						"<td>\n"+
						"<b>"+post.getAutor()+"</b>\n"+
						"<p>"+post.getMensagem()+"</p>\n");
			List<Comentario> coments = post.getComentarios();
			if (!coments.isEmpty()) {
				out.println("<table border=\"1\" width=\"100%\" bgcolor=\"lightgray\">");
			}
			for (Comentario coment : coments) {
				out.println("<tr><td><b>"+coment.getAutor()+"</b> "+coment.getTexto()+"</td></tr>");
			}
			if (!coments.isEmpty()) {
				out.println("</table>\n");
			}
			if(post.ifAlguemCurtiu()) {
				out.println("<b>Alguém curtiu!</b>");
			} else {
				out.println("<a href=\"/exemploServlet/curtir?post="+post.getId()+"\">curtir</a>");
			}
			out.println("&nbsp;&nbsp;<a href=\"/exemploServlet/formComentar?post="+post.getId()+"\">comentar</a>"+ 
						"<hr/>\n"+
						"</td>\n"+
						"<td>&nbsp;</td></tr>");
		}
		out.println("</table>");
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
